package com.wireless.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wireless.base.BaseAction;
import com.wireless.entity.Purchase;
import com.wireless.entity.Usertbl;
import com.wireless.service.UserService;

@Namespace("/user")
@ParentPackage("json-default")
@Controller
public class UserAction extends BaseAction {
	@Autowired
	private UserService userService;
	private Usertbl user;
	private Usertbl loginedUser;
	private String code;
	private List<Usertbl> userList;
	@Action(value = "login", results = {
			@Result(name = "success", type = "redirect", location = "../views/index.jsp"),
			@Result(name = "failure", type = "redirect", location = "../views/login.jsp") })
	public String login() {
		loginedUser = userService.checkUser(user);
		if (loginedUser != null) {
			session.put("userid", user.getId());
			code = "1";
			return SUCCESS;
		} else {
			session.put("loginFail", "账号或密码错误");
			code = "0";
			return "failure";
		}
	}

	@Action(value = "userList", results = { @Result(name = "success", location = "/views/grzx.jsp") })
	public String userList() {
		userList=userService.userList();
		return SUCCESS;
	}

	@Action(value = "addUser", results = { @Result(name = "success", location = "/views/grzx.jsp") })
	public String addUser() {
		userService.addUser(user);
		userList=userService.userList();
		return SUCCESS;
	}

	public Usertbl getLoginedUser() {
		return loginedUser;
	}

	public void setLoginedUser(Usertbl loginedUser) {
		this.loginedUser = loginedUser;
	}

	public Usertbl getUser() {
		return user;
	}

	public void setUser(Usertbl user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public List<Usertbl> getUserList() {
		return userList;
	}

	public void setUserList(List<Usertbl> userList) {
		this.userList = userList;
	}

}
